﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace XinChen
{
    class MPlayer
    {
        public string filename_Full;
        public string filename_Path;
        public string filename;
        public string filename_noExt;
        public string filename_SRT;

        public int DisplayHandle = 0;
        public int FontSize = 3;
        public DataReceivedEventHandler HandleOutput;

        string mplayer_exe;
        string subfont;

        Process mplayer;
        string mplayer_args;

        public MPlayer(string src_file)
        {
            if (!File.Exists(src_file))
            {
                MessageBox.Show("媒体文件不存在，无法播放！", "MPlayer Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //文件不存在，所有参数为null
                return;
            }

            mplayer_exe = Application.StartupPath + @"\Lib\mplayer.exe";
            subfont = Application.StartupPath + @"\Lib\msyh.ttf";
            if (!File.Exists(mplayer_exe) || !File.Exists(subfont))
            {
                MessageBox.Show("程序文件丢失，无法播放！", "MPlayer Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //文件不存在，所有参数为null
                return;
            }

            filename_Full = src_file;
            filename_Path = filename_Full.Substring(0, filename_Full.LastIndexOf('\\') + 1);
            filename = filename_Full.Substring(filename_Full.LastIndexOf('\\') + 1);
            if (filename.LastIndexOf('.') != -1)
            {
                filename_noExt = filename.Substring(0, filename.LastIndexOf('.'));
            }
            else
            {
                filename_noExt = filename;
            }

            filename_SRT = filename_Path + filename_noExt + ".srt";
            if (!File.Exists(filename_SRT))
            {
                filename_SRT = null;
            }
        }

        public void play()
        {
            mplayer = new Process();
            mplayer.StartInfo.FileName = mplayer_exe;

            mplayer.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            mplayer.StartInfo.CreateNoWindow = true;
            mplayer.StartInfo.UseShellExecute = false;
            mplayer.StartInfo.RedirectStandardInput = true;
            mplayer.StartInfo.RedirectStandardOutput = true;
            mplayer.StartInfo.ErrorDialog = false;

            //-nofs: no fullscreen
            //-noquiet: 显示详细状态信息（ A:   0.7 V:   0.6 A-V:  0.068 ...）
            //-identify: Show file parameters
            //-slave -nomouseinput -vo direct3d -ao dsound
            //-wid will tell MPlayer to show output inisde our panel
            mplayer_args = "-nofs -noquiet -identify -slave -nomouseinput -vo direct3d -ao dsound";

            if (DisplayHandle != 0)
            {
                mplayer_args += " -wid " + DisplayHandle;
            }
            if (HandleOutput != null)
            {
                mplayer.OutputDataReceived += HandleOutput;
            }

            mplayer_args += " -subcp gb2312 -subfont \"" + subfont + "\" -subpos 99 -subfont-text-scale " + FontSize;
            if (filename_SRT != null)
            {
                mplayer_args += " -sub \"" + filename_SRT + "\"";
            }

            mplayer_args += " \"" + filename_Full + "\"";

            //其他参数测试
            //mplayer_args += " -ss 13.647 -endpos 3.7";

            mplayer.StartInfo.Arguments = mplayer_args;
            mplayer.Start();
            mplayer.BeginOutputReadLine();
        }
        public void play(float startPos)
        {
            if (mplayer == null || mplayer.HasExited)
            {
                play();
            }
            //mplayer.StandardInput.WriteLine("Pause");
            mplayer.StandardInput.WriteLine("Seek " + startPos.ToString() + " 2");
        }
        public void doCommand(string command)
        {
            if (mplayer != null)
            {
                mplayer.StandardInput.WriteLine(command);
            }
        }

        public void Close()
        {
            mplayer.StandardInput.WriteLine("quit");
            mplayer.CloseMainWindow();
            //mplayer.Dispose();
        }
    }
}